package com.hm.cms.websit;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hm.cms.def.LeiphoneTypeEnum;
import com.hm.cms.service.ArticleService;
import com.hm.cms.service.TermsService;
import com.hm.cms.util.WebSitUtil;
import com.hm.cms.vo.HcmsArticleVo;
import com.hm.cms.vo.HcmsTermsVo;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年11月28日 上午11:16:23
 * @version 1.0
 * @describe 
 */
@Slf4j
@Component
public class LeiphoneHandler {
	
	@Autowired
	private TermsService termsService;

	@Autowired
	private ArticleService articleService;
	
	public void handler() {
		for (LeiphoneTypeEnum type : LeiphoneTypeEnum.values()) {
			mainDirectory(type.getTermTaxonomy(),type.getWebTag(),"1");
			WebSitUtil.wating3();
		}
	}

	public static void main(String[] args) throws Exception{
		String uri = "https://www.leiphone.com/news/201706/H2PUINRPl9XKrC1R.html";
		Document document = Jsoup.connect(uri).ignoreContentType(true)
				.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
		
		Elements contentListEls = document.getElementsByClass("article-template");
		if(CommonUtil.isEmpty(contentListEls)) {
			return;
		}
		Element articleContenEl = contentListEls.get(0);
		
		Elements titles = articleContenEl.getElementsByClass("headTit");
		if(CommonUtil.isEmpty(titles)) {
			return;
		}
		String title = titles.get(0).html();
		
		System.out.println("title-->"+title);
		
		
		Elements contents = articleContenEl.getElementsByClass("lph-article-comView");
		if(CommonUtil.isEmpty(contents)) {
			return;
		}
		Element contentEl = contents.get(0);
		if(CommonUtil.isEmpty(contentEl)) {
			return;
		}
		Elements elementsByTag = contentEl.getElementsByTag("p");
		StringBuilder content = new StringBuilder();
		for(int i = 0 ; i < elementsByTag.size() -1 ;i++) {
			content.append(elementsByTag.get(i)).append(System.lineSeparator());
		}
		System.out.println(content.toString());
	}
	
	private void loadArticle(String uri,String imgUri,String termTaxonomy,String operationUserId) {
		try {
			if (articleService.checkArticleSourceUri(uri)) {
				return;
			}
			
//			String uri = "https://www.leiphone.com/news/201706/H2PUINRPl9XKrC1R.html";
			Document document = Jsoup.connect(uri).ignoreContentType(true)
					.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
			
			String description = document.select("meta[name=description]").get(0).attr("content");
			String keyStr = document.select("meta[name=keywords]").get(0).attr("content");
			System.out.println(description);
			System.out.println(keyStr);
			
			List<String> termIds = new ArrayList<String>();
			String[] keys = keyStr.split(",");
			for (String key : keys) {
				if(CommonUtil.isEmpty(key)) {
					continue;
				}
				HcmsTermsVo hcmsTermsVo = termsService.getHcmsTermsByTermName(key);
				if (CommonUtil.isNotEmpty(hcmsTermsVo)) {
					termIds.add(hcmsTermsVo.getId());
					continue;
				}

				HcmsTermsVo termsVo = new HcmsTermsVo();
				termsVo.setTermName(key);
				termsVo.setTermCode(key);
				termsVo.setTermOrder(-1);
				termIds.add(termsService.addTerms(termsVo, operationUserId));
			}
			
			
			Elements contentListEls = document.getElementsByClass("article-template");
			if(CommonUtil.isEmpty(contentListEls)) {
				return;
			}
			Element articleContenEl = contentListEls.get(0);
			
			Elements titles = articleContenEl.getElementsByClass("headTit");
			if(CommonUtil.isEmpty(titles)) {
				return;
			}
			String title = titles.get(0).html();
			
			System.out.println("title-->"+title);
			
			
			Elements contents = articleContenEl.getElementsByClass("lph-article-comView");
			if(CommonUtil.isEmpty(contents)) {
				return;
			}
			Element contentEl = contents.get(0);
			if(CommonUtil.isEmpty(contentEl)) {
				return;
			}
			Elements elementsByTag = contentEl.getElementsByTag("p");
			StringBuilder content = new StringBuilder();
			for(int i = 0 ; i < elementsByTag.size() -3 ;i++) {
				content.append(elementsByTag.get(i)).append(System.lineSeparator());
			}
			
			if(StringUtil.isBlankOrNull(description)) {
            	description = StringUtil.dislodgeAllBlank(WebUtil.clearHtmlTags(content.toString())).substring(0, 150);
            }
			
			System.out.println(content.toString());
			
			HcmsArticleVo articleVo = new HcmsArticleVo();
			{
				articleVo.setTermIds(termIds);
				articleVo.setTermTayIds(Arrays.asList(termTaxonomy));
				articleVo.setArticleContent(content.toString());
				articleVo.setBreviaryIcon(imgUri);
				articleVo.setAuthorName("小旋风");
				articleVo.setReleaseTime(System.currentTimeMillis());
				articleVo.setTitle(title);
				articleVo.setKeywords(StringUtil.isBlankOrNull(keyStr)?title:keyStr);
				articleVo.setSourceName("雷锋网");
				articleVo.setSourceUri(uri);
				articleVo.setArticleOrder((int) articleService.getArtilcleMaxOrder());
				articleVo.setArticleExcerpt(description);
			}
			articleService.addArticle(articleVo, operationUserId);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	
	private void mainDirectory(String termTaxonomy,String uri,String operationUserId) {
		try {
			for(int page = 3;page >= 1;page --) {
				//https://www.leiphone.com/category/transportation/page/2
//				String uri = "https://www.leiphone.com/category/ai/page/50";
				Document document = Jsoup.connect(MessageFormat.format(uri, page)).ignoreContentType(true)
						.userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)").get();
				
				
				Elements pageListEls = document.getElementsByClass("lph-pageList");
				if(CommonUtil.isEmpty(pageListEls)) {
					return;
				}
				Elements elements = pageListEls.get(0).getElementsByClass("box");
				for (Element el : elements) {
					Elements articleEls = el.getElementsByClass("headTit");
					if(CommonUtil.isEmpty(articleEls)) {
						continue;
					}
					Element articleEl = articleEls.get(0);
					if(CommonUtil.isEmpty(articleEl)) {
						continue;
					}
					String articleUri = articleEl.attr("href");
					System.out.println("articleUri-->"+articleUri);
					
					String imgUri = null;
					Elements imgEls = el.getElementsByClass("lazy");
					if(CommonUtil.isNotEmpty(imgEls)) {
						imgUri = imgEls.get(0).attr("data-original");
					}
					System.out.println("imgUri-->"+imgUri);
					
					loadArticle(articleUri, imgUri, termTaxonomy, operationUserId);
					WebSitUtil.wating3();
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	}
}
